package com.learn.project2.service.admin.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.*;
import com.learn.project2.model.bo.admin.goods.GoodsCreateBO;
import com.learn.project2.model.bo.admin.goods.GoodsCreateBOProducts;
import com.learn.project2.model.bo.admin.goods.GoodsDeleteBO;
import com.learn.project2.model.bo.admin.goods.GoodsUpdateBO;
import com.learn.project2.model.vo.admin.goods.GoodsCatAndBrandVO;
import com.learn.project2.model.vo.admin.goods.GoodsDetailVO;
import com.learn.project2.model.vo.admin.goods.GoodsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;

/**
 * @author 曹源
 * @version 1.0
 * @description: 商品管理/商品列表 Admin/goods的方法实现
 * @date 2022/1/8 10:20
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketCartMapper marketCartMapper;

    /**
     * @description: 商品管理/商品列表/查询 Admin/goods/list的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 10:21
     */
    @Override
    public GoodsListVO selectlist(Integer page, Integer limit, Integer goodsSn, String name, String sort, String order, Integer goodsId) {
        GoodsListVO goodsListVO = new GoodsListVO();

        //推广管理/优惠券管理发送的limit=0的请求
        if (limit == 0) {
            page = 0;
            sort = "add_time";
            order = "asc";
            MarketGoodsExample example = new MarketGoodsExample();
            // 提供了一个条件List → Criterion
            //criteria中包含List<Criterion> 使用Criteria提供的方法就是向List塞入条件
            //排序的依赖条件
            example.setOrderByClause(sort + "\n" + order);
            MarketGoodsExample.Criteria criteria = example.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(example);
            //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
            PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
            goodsListVO.setPage(page);
            goodsListVO.setPages(0);
            goodsListVO.setLimit(limit);
            goodsListVO.setList(pageInfo.getList());
            goodsListVO.setTotal((int) pageInfo.getTotal());
            return goodsListVO;
        }

        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //根据keyword和url做一个模糊查询
        MarketGoodsExample example = new MarketGoodsExample();
        // 提供了一个条件List → Criterion
        //criteria中包含List<Criterion> 使用Criteria提供的方法就是向List塞入条件
        //排序的依赖条件
        example.setOrderByClause(sort + "\n" + order);
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        //and 列名 条件
        if (goodsSn != null) criteria.andGoodsSnLike("%" + goodsSn + "%");
        if (goodsId != null) criteria.andIdEqualTo(goodsId);
        if (name != null) criteria.andNameLike("%" + name + "%");
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(example);
        //分页
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
        //映射赋值VO
        goodsListVO.setTotal((int) pageInfo.getTotal());
        goodsListVO.setPages(pageInfo.getPages());
        goodsListVO.setPage(page);
        goodsListVO.setLimit(limit);
        goodsListVO.setList(marketGoods);
//        goodsListVO.setList(pageInfo.getList());
        return goodsListVO;
    }

    /**
     * @description: 商品管理/商品列表/添加 Admin/goods/create的接口实现方法
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 19:00
     */
    @Override
    public void insert(GoodsCreateBO goodsCreateBO) {
        //goods表的插入
        MarketGoods good = goodsCreateBO.getGoods();
        good.setAddTime(new Date());
        good.setUpdateTime(new Date());
        marketGoodsMapper.insertSelective(good);
        Integer goodsId = good.getId();
        //attributes表的插入
        for (MarketGoodsAttribute attribute : goodsCreateBO.getAttributes()) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            marketGoodsAttributeMapper.insertSelective(attribute);
        }
        //specifications表的插入
        for (MarketGoodsSpecification specification : goodsCreateBO.getSpecifications()) {
            specification.setGoodsId(goodsId);
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            marketGoodsSpecificationMapper.insertSelective(specification);
        }
        //products表的插入
        for (GoodsCreateBOProducts product : goodsCreateBO.getProducts()) {
            MarketGoodsProduct marketGoodsProduct = new MarketGoodsProduct();
            marketGoodsProduct.setId(product.getId());
            marketGoodsProduct.setGoodsId(goodsId);
            marketGoodsProduct.setNumber(Integer.parseInt(product.getNumber()));
            marketGoodsProduct.setPrice(BigDecimal.valueOf(Long.parseLong(product.getPrice())));
            marketGoodsProduct.setUrl(product.getUrl());
            marketGoodsProduct.setSpecifications(product.getSpecifications());
            marketGoodsProduct.setId(goodsId);
            marketGoodsProduct.setAddTime(new Date());
            marketGoodsProduct.setUpdateTime(new Date());
            marketGoodsProductMapper.insertSelective(marketGoodsProduct);
        }
    }

    /**
     * @description: 商品管理/商品列表/编辑 Admin/goods/update的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 17:30
     */
    @Override
    public void updateGoods(GoodsUpdateBO goodsUpdateBO) {
        //goods表的更新
        MarketGoods good = goodsUpdateBO.getGoods();
        good.setUpdateTime(new Date());
        marketGoodsMapper.updateByPrimaryKey(goodsUpdateBO.getGoods());
        //attributes表的更新
        for (MarketGoodsAttribute attribute : goodsUpdateBO.getAttributes()) {
            attribute.setUpdateTime(new Date());
            marketGoodsAttributeMapper.updateByPrimaryKey(attribute);
        }
        //specifications表的更新
        for (MarketGoodsSpecification specification : goodsUpdateBO.getSpecifications()) {
            specification.setUpdateTime(new Date());
            marketGoodsSpecificationMapper.updateByPrimaryKey(specification);
        }
        //products表的更新
        for (MarketGoodsProduct product : goodsUpdateBO.getProducts()) {
            product.setUpdateTime(new Date());
            marketGoodsProductMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * @description: 商品管理/商品列表/删除 Admin/goods/delete的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 16:28
     */
    @Override
    public void delete(GoodsDeleteBO goodsDeleteBO) {
        //goods表的删除
        Integer goodsId = goodsDeleteBO.getId();
        MarketGoods marketGoods = new MarketGoods();
        marketGoods.setId(goodsId);
        marketGoods.setDeleted(true);
        marketGoodsMapper.updateByPrimaryKeySelective(marketGoods);
        //attribute表的删除
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = marketGoodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        MarketGoodsAttribute marketGoodsAttribute = new MarketGoodsAttribute();
        marketGoodsAttribute.setDeleted(true);
        marketGoodsAttributeMapper.updateByExampleSelective(marketGoodsAttribute,marketGoodsAttributeExample);
        //specification表的删除
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria1 = marketGoodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(goodsId);
        MarketGoodsSpecification marketGoodsSpecification = new MarketGoodsSpecification();
        marketGoodsSpecification.setDeleted(true);
        marketGoodsSpecificationMapper.updateByExampleSelective(marketGoodsSpecification,marketGoodsSpecificationExample);
        //product表的删除
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = marketGoodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(goodsId);
        MarketGoodsProduct marketGoodsProduct = new MarketGoodsProduct();
        marketGoodsProduct.setDeleted(true);
        marketGoodsProductMapper.updateByExampleSelective(marketGoodsProduct,marketGoodsProductExample);
        //购物车删除
        MarketCartExample marketCartExample = new MarketCartExample();
        marketCartExample.createCriteria().andGoodsIdEqualTo(goodsId);
        MarketCart marketCart = new MarketCart();
        marketCart.setDeleted(true);
        marketCartMapper.updateByExampleSelective(marketCart,marketCartExample);
    }

    /**
     * @description: 商品管理/商品列表/详细情况 Admin/goods/detail的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 15:01
     */
    @Override
    public GoodsDetailVO selectDetail(Integer id) {
        GoodsDetailVO goodsDetailVO = new GoodsDetailVO();

        //查询goods并完成赋值
        MarketGoods marketGood = marketGoodsMapper.selectByPrimaryKey(id);
        //查询赋值text类型的detail变量
        MarketGoods marketGood1 = marketGoodsMapper.selectByPrimaryKey(marketGood.getId());
        marketGood.setDetail(marketGood1.getDetail());
        goodsDetailVO.setGoods(marketGood);

        //查询categoryIds并完成赋值
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria3 = marketCategoryExample.createCriteria();
        criteria3.andDeletedEqualTo(false);
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(marketGood.getCategoryId());
        List<Integer> integers = new ArrayList<>();
        integers.add(marketCategory.getPid());
        integers.add(marketCategory.getId());
        goodsDetailVO.setCategoryIds(integers);

        //查询attributes并完成赋值
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = marketGoodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        goodsDetailVO.setAttributes(marketGoodsAttributes);

        //查询specification并完成赋值
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria1 = marketGoodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);
        goodsDetailVO.setSpecifications(marketGoodsSpecifications);

        //查询products并完成赋值
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = marketGoodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        goodsDetailVO.setProducts(marketGoodsProducts);

        return goodsDetailVO;
    }

    /**
     * @description: 商品管理/商品列表/类别和商标信息 Admin/goods/catAndBrand的接口方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-08 11:23
     */
    @Override
    public GoodsCatAndBrandVO selectCatAndBrand() {
        //创建catAndBrandVO输出映射
        GoodsCatAndBrandVO catAndBrandVO = new GoodsCatAndBrandVO();

//        给brandList赋值
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria2 = marketBrandExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
        List<GoodsCatAndBrandVO.BrandListDTO> brandListDTOS = new ArrayList<>();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        for (MarketBrand marketBrand : marketBrands) {
            GoodsCatAndBrandVO.BrandListDTO brandListDTO = new GoodsCatAndBrandVO.BrandListDTO();
            brandListDTO.setValue(marketBrand.getId());
            brandListDTO.setLabel(marketBrand.getName());
            brandListDTOS.add(brandListDTO);
        }
        //创建CategoryList
        List<GoodsCatAndBrandVO.CategoryListDTO> categoryListDTOS = new ArrayList<>();
        MarketCategoryExample marketBrandExample1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketBrandExample1.createCriteria();
        criteria.andPidEqualTo(0).andDeletedEqualTo(false);
        //pid=0的ID作为原始分组，查询出pid=0的结果集合的ID；
        // 以查询出pid=0的结果集合的ID作为新的查询的Pid的条件，进行分组赋值。
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketBrandExample1);
        for (MarketCategory marketCategory : marketCategories) {
            //给categoryList赋值
            GoodsCatAndBrandVO.CategoryListDTO categoryListDTO = new GoodsCatAndBrandVO.CategoryListDTO();
            categoryListDTO.setValue(marketCategory.getId());
            categoryListDTO.setLabel(marketCategory.getName());
            //给children赋值
            List<GoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO> childrenDTOS = new ArrayList<>();
            MarketCategoryExample marketBrandExample1List = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = marketBrandExample1List.createCriteria();
            criteria1.andPidEqualTo(marketCategory.getId());
            List<MarketCategory> childrens = marketCategoryMapper.selectByExample(marketBrandExample1List);
            for (MarketCategory children : childrens) {
                GoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO childrenDTO = new GoodsCatAndBrandVO.CategoryListDTO.ChildrenDTO();
                childrenDTO.setValue(children.getId());
                childrenDTO.setLabel(children.getName());
                childrenDTOS.add(childrenDTO);
            }
            categoryListDTO.setChildren(childrenDTOS);
            categoryListDTOS.add(categoryListDTO);
        }
        //最终赋值
        catAndBrandVO.setBrandList(brandListDTOS);
        catAndBrandVO.setCategoryList(categoryListDTOS);
        return catAndBrandVO;
    }
}
